<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Program extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'));
        $this->data['controller'] = 'program';
        $this->load->model('Mprogram');
    }
    
    public function selectGameshow()
    {
        $this->data['campaign'] = $this->Mprogram->getCampaign();
        //echo '<pre>';
        //die(print_r($this->data['campaign']));
        
        $this->data['action'] = 'selectgameshow';
        $this->load->view('template/layout', $this->data);
    }
    
    
    public function operation()
    {
        $this->data['id'] = $campaign_id = $this->getParamInt('campaign');
        $data = $this->Mprogram->getQuestion($campaign_id);
        $this->data['data'] = $data['arr'];
        $this->data['count'] = $data['count'];
        //$this->data['bxh'] = $data['bxh'];
        //echo '<pre>';
        //die(print_r($this->data['bxh']));
        
        $this->data['action'] = 'operation';
        $this->load->view('template/layout', $this->data);
    }
    
    public function creatnewSession()
    {
        $this->data['data'] = $this->Mprogram->newSession();
        
        die($this->data['data'].'');
    }
    
    public function updateStatus()
    {
        $delay = $this->getParamInt('delay');
        $campaign = $this->getParamInt('campaign');
        $question = $this->getParamInt('question');
        $status = $this->Mprogram->updateCampQuest($campaign, $question);
        $this->data['data'] = $this->Mprogram->updateTimeSend($delay);
        die(json_encode($this->Mprogram->getBxh()));
    }
    
    public function checkanswer()
    {
        die(json_encode($this->Mprogram->fillterPlayer()));
    }

    public function index()
    {
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(3,3);
        $offset = (($this->getParamUri(3,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(3,3) - 1) * $config['per_page']) : 0;
        $data = $this->Mprogram->getIndex($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,3);
        $config['suffix'] = '.html';
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
		$this->data['action'] = 'index';
        $this->load->view('template/layout',$this->data);
    }
    
    public function addProgram()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('title', 'Tên chương trình', 'required');
        
        $this->data['data'] = '';
        if($this->form_validation->run() === TRUE)
        {
            $title = $this->getParamString('title');
            $this->data['data'] = $this->Mprogram->addThisProgram($title);
            if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm chương trình thành công!");window.location.href="'.$this->data['baseurl'].'manage/program/add-program/";</script>');
            }
        }
        
        $this->data['action'] = 'addprogram';
        $this->load->view('template/layout',$this->data);
    }
    
    public function editProgram()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('title', 'Tên chương trình', 'required');
        
        $id = $this->getParamUri(4,0);
        $this->data['data'] = $this->Mprogram->getThisProgram($id);
        if($this->form_validation->run() === TRUE)
        {
            $title = $this->getParamString('title');
            $this->data['data'] = $this->Mprogram->editThisProgram($title, $id);
            if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa thông tin thành công!");window.location.href="'.$this->data['baseurl'].'manage/program/edit/'.$id.'";</script>');
            }
        }
        
        $this->data['action'] = 'editprogram';
        $this->load->view('template/layout',$this->data);
    }
    
    public function deleteProgram()
    {
        $id = $this->getParamInt('id');
        $this->data['delete'] = $this->Mprogram->deleteThisProgram($id);
    }
    
    public function detailProgram()
    {
        $id = $this->data['id'] = $this->getParamUri(4,0);
        $this->data['data'] = $this->Mprogram->getDetailProgram($id);
        
        $this->data['action'] = 'detailprogram';
        $this->load->view('template/layout',$this->data);
    }
    
    public function addQuestion()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('stt', 'Số thứ tự', 'required');
        $this->form_validation->set_rules('question', 'Nội dung câu hỏi', 'required');
        $this->form_validation->set_rules('choice-1', 'Đáp án 1', 'required');
        $this->form_validation->set_rules('choice-2', 'Đáp án 2', 'required');
        $this->form_validation->set_rules('choice-3', 'Đáp án 3', 'required');
        $this->form_validation->set_rules('choice-4', 'Đáp án 4', 'required');
        $this->form_validation->set_rules('answer', 'Đáp án đúng', 'required');
        
        $this->data['data'] = '';
        $this->data['id'] = $id_campaign = $this->getParamUri(4,0);
        $this->data['campaign'] = $this->Mprogram->getCampaign();
        if($this->form_validation->run() === TRUE)
        {
            $stt = $this->getParamString('stt');
            $question = $this->getParamString('question');
            $choice1 = $this->getParamString('choice-1');
            $choice2 = $this->getParamString('choice-2');
            $choice3 = $this->getParamString('choice-3');
            $choice4 = $this->getParamString('choice-4');
            $answer = $this->getParamString('answer');
            
            $this->data['data'] = $this->Mprogram->addThisQuestion($id_campaign,$stt,$question,$choice1,$choice2,$choice3,$choice4,$answer);
            if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm câu hỏi thành công!");window.location.href="'.$this->data['baseurl'].'manage/question/add-question/'.$id_campaign.'";</script>');
            }
        }
        
        $this->data['action'] = 'addquestion';
        $this->load->view('template/layout',$this->data);
    }
    
    public function editQuestion()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('question', 'Nội dung câu hỏi', 'required');
        $this->form_validation->set_rules('choice-1', 'Đáp án 1', 'required');
        $this->form_validation->set_rules('choice-2', 'Đáp án 2', 'required');
        $this->form_validation->set_rules('choice-3', 'Đáp án 3', 'required');
        $this->form_validation->set_rules('choice-4', 'Đáp án 4', 'required');
        $this->form_validation->set_rules('answer', 'Đáp án đúng', 'required');
        
        $id = $this->getParamUri(4,0);
        $this->data['data'] = $this->Mprogram->getThisQuestion($id);
        $this->data['campaign'] = $this->Mprogram->getCampaign();
        if($this->form_validation->run() === TRUE)
        {
            $question = $this->getParamString('question');
            $choice1 = $this->getParamString('choice-1');
            $choice2 = $this->getParamString('choice-2');
            $choice3 = $this->getParamString('choice-3');
            $choice4 = $this->getParamString('choice-4');
            $answer = $this->getParamString('answer');
            $id_campaign = $this->getParamInt('id_campaign');
            
            $this->data['data'] = $this->Mprogram->editThisQuestion($id,$id_campaign,$question,$choice1,$choice2,$choice3,$choice4,$answer);
            if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa câu hỏi thành công!");window.location.href="'.$this->data['baseurl'].'manage/question/edit/'.$id.'";</script>');
            }
        }
        
        $this->data['action'] = 'editquestion';
        $this->load->view('template/layout',$this->data);
    }
    
    public function deleteQuestion()
    {
        $id = $this->getParamInt('id');
        $this->data['delete'] = $this->Mprogram->deleteThisQuestion($id);
    }
    
    public function stopgame()
    {
        die(json_encode($this->Mprogram->copyScore($this->getParamInt('campaign'))));
    }
}
